package org.lcc.config;

import org.lcc.job.ExamNotificationJob;
import org.lcc.job.ExamStatusUpdateJob;
import org.lcc.job.ScoreAnalysisJob;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 配置Quartz调度器的类
 * 定义了三个任务及其触发器：考试通知、考试状态更新和成绩分析
 */
@Configuration
public class QuartzConfig {

    /**
     * 创建考试通知任务的详细信息
     *
     * @return JobDetail对象，包含任务的详细信息
     */
    @Bean
    public JobDetail examNotificationJobDetail() {
        return JobBuilder.newJob(ExamNotificationJob.class)
                .withIdentity("examNotificationJob", "examGroup")
                .storeDurably()
                .build();
    }

    /**
     * 创建考试通知任务的触发器
     *
     * @return Trigger对象，定义了任务的执行时间表
     */
    @Bean
    public Trigger examNotificationTrigger() {
        // 每分钟触发一次
        return TriggerBuilder.newTrigger()
                .forJob(examNotificationJobDetail())
                .withIdentity("examNotificationTrigger", "examGroup")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(1)
                        .repeatForever())
                .build();
    }
    /**
     * 新增考试状态更新任务的JobDetail
     *
     * @return JobDetail对象，包含考试状态更新任务的详细信息
     */
    @Bean
    public JobDetail examStatusUpdateJobDetail() {
        return JobBuilder.newJob(ExamStatusUpdateJob.class)
                .withIdentity("examStatusUpdateJob", "statusUpdateGroup")
                .storeDurably()
                .build();
    }

    /**
     * 新增考试状态更新任务的Trigger
     *
     * @return Trigger对象，定义了考试状态更新任务的执行时间表
     */
    @Bean
    public Trigger examStatusUpdateTrigger() {
        return TriggerBuilder.newTrigger()
                .forJob(examStatusUpdateJobDetail())
                .withIdentity("examStatusUpdateTrigger", "statusUpdateGroup")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(60) //一分钟
                        .repeatForever())
                .build();
    }
    /**
     * 新增成绩分析任务的JobDetail
     *
     * @return JobDetail对象，包含成绩分析任务的详细信息
     */
    @Bean
    public JobDetail scoreAnalysisJobDetail() {
        return JobBuilder.newJob(ScoreAnalysisJob.class)
                .withIdentity("scoreAnalysisJob", "analysisGroup")
                .storeDurably()
                .build();
    }

    /**
     * 新增成绩分析任务的Trigger
     *
     * @return Trigger对象，定义了成绩分析任务的执行时间表
     */
    @Bean
    public Trigger scoreAnalysisTrigger() {
        return TriggerBuilder.newTrigger()
                .forJob(scoreAnalysisJobDetail())
                .withIdentity("scoreAnalysisTrigger", "analysisGroup")
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(300) // 5分钟执行一次
                        .repeatForever())
                .build();
    }
}
